
package com.google.code.vh;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

public class LoginWindow extends Window
{
  private Button    btnLogin = new Button( "Login" );
  private TextField login    = new TextField( "Username" );
  private TextField password = new TextField( "Password" );

  public LoginWindow()
  {
    super( "Authentication Required!" );
    setName( "login" );
    initUI();
  }

  private void initUI()
  {
    password.setSecret( true );

    addComponent( new Label( "Please login in order to use the application" ) );
    addComponent( new Label() );
    addComponent( login );
    addComponent( password );
    addComponent( btnLogin );

    btnLogin.addListener( new Button.ClickListener() {
      @Override
      public void buttonClick( Button.ClickEvent event )
      {
        try
        {
          App.getInstance().authenticate( (String) login.getValue(), (String) password.getValue() );
          open( new ExternalResource( App.getInstance().getURL() ) );
        }
        catch ( Exception e )
        {
          e.printStackTrace();
          showNotification( e.toString() );
        }
      }
    } );
  }
}
